package com.cugmarket.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cugmarket.constant.MessageConstant;
import com.cugmarket.entity.ProductCollect;
import com.cugmarket.entity.ProductInfo;
import com.cugmarket.entity.User;
import com.cugmarket.mapper.ProductCollectMapper;
import com.cugmarket.service.ProductCollectService;
import com.cugmarket.utils.UserHolder;
import com.cugmarket.vo.PageVO;
import com.cugmarket.vo.ProductInfoVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProductCollectServiceImpl extends ServiceImpl<ProductCollectMapper, ProductCollect> implements ProductCollectService {
    @Resource
    private ProductCollectMapper productCollectMapper;
    @Resource
    private ProductInfoServiceImpl productInfoService;

    /**
     * 查询所有收藏
     * @return
     */
    @Override
    public List<ProductCollect> selectList() {
        return productCollectMapper.selectList(null);
    }

    @Override
    public List<ProductCollect> getPCByUserId(String userId) {
        // 根据 userId 进行条件查询
        LambdaQueryWrapper<ProductCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductCollect::getUserId, userId)
                .orderByDesc(ProductCollect::getCreateTime);
        return productCollectMapper.selectList(queryWrapper);
    }

    /**
     * 添加收藏
     * @param productCollect 商品信息
     * @return
     */
    @Override
    @Transactional
    public Boolean addProductCollect(ProductCollect productCollect) {
        // 获取当前用户
        String userId = UserHolder.getUser();
        productCollect.setUserId(userId);

        // 查询商品信息
        ProductInfoVO productInfoVO = productInfoService.getByProductId(productCollect.getProductId());
        if (productInfoVO == null) {
            throw new RuntimeException(MessageConstant.IS_EMPTY);
        }
        ProductInfo productInfo = new ProductInfo();
        BeanUtil.copyProperties(productInfoVO, productInfo);

        // 商品点赞数加一
        productInfo.setLikeCount(productInfo.getLikeCount() + 1);
        // 更新商品信息
        boolean saveProductInfo = productInfoService.updateById(productInfo);
        // 添加收藏
        boolean saveProductCollect = save(productCollect);
        return saveProductInfo && saveProductCollect;
    }

    /**
     * 删除收藏
     * @param productId 商品id
     * @return
     */
    @Override
    public Boolean removeProductCollect(String productId) {
        // 查询商品信息
        ProductInfoVO productInfoVO = productInfoService.getByProductId(productId);
        if (productInfoVO == null) {
            throw new RuntimeException(MessageConstant.IS_EMPTY);
        }
        ProductInfo productInfo = new ProductInfo();
        BeanUtil.copyProperties(productInfoVO, productInfo);

        // 商品点赞数加一
        if (productInfo.getLikeCount()>0){
            productInfo.setLikeCount(productInfo.getLikeCount() - 1);
        }
        // 更新商品信息
        boolean saveProductInfo = productInfoService.updateById(productInfo);
        String userId = UserHolder.getUser();
        LambdaQueryWrapper<ProductCollect> queryWrapper = new LambdaQueryWrapper<ProductCollect>().
                eq(ProductCollect::getUserId, userId)
                .eq(ProductCollect::getProductId, productId);
        boolean removed = remove(queryWrapper);
        return saveProductInfo && removed;
    }

}
